const models = require('../db/db');
const express = require('express');
const router = express.Router();
const mysql = require('mysql');
const $sql = require('../db/sqlMap');

const conn = mysql.createConnection(models.mysql);
conn.connect();
// 查询全部
router.post('/findAll',(req,res)=>{
	const params = req.body;
	const sel= $sql.collect.select+ " where id = '" + params.user_id + "'";	
	conn.query(sel,params.user_id,(error, results)=>{
		if (error) {
			throw error;
		}
		if (results!=null) {
			res.send(results);  // -1 表示查询不到，用户不存在，即邮箱填写错误
		} 
	})
});

// 删除接口
router.post('/delCollect',(req,res)=>{
	const user = req.body;
	const sel_email = $sql.collect.delete + " where collect_id = '" + user.id + "'";
	conn.query(sel_email,user.id, (error, results)=>{
		if (error) {
			throw error;
		}else{
			res.send({msg:'ok'}); 
		}
		
	})
});

// 加入收藏接口
router.post('/addCollect', (req, res) => {
	const params = req.body;
	const add_sql = $sql.collect.add;
	const sel= $sql.collect.select+ " where collect_name = '" + params.collect_name + "'and id='"+params.id+"'";	
	
	conn.query(sel, [params.collect_name,params.id],(err, rst) => {
		if (err) {
			console.log(err);
		} 	
		if(rst[0]!=null){			
			res.send({msg:'1'});
		}else{
			conn.query(add_sql, [params.collect_name, params.collect_img, params.collect_price,params.collect_discount, params.goods_id, params.id], (err, rst) => {
				if (err) {
					console.log(err);
				} else{
					res.send({msg:'0'});
				}
			});	
		}
	});	

});


module.exports = router;